<?php

/*
	Class: BlogCategories

	About: Author
		Jaybill McCarthy

	About: License
		<http://communit.as/docs/license>
*/
class BlogCategories extends Blog_Db_Table {

	protected $_name = 'blog_categories';

	protected $_primary = 'id';
	
	function getAllCategories($locale = "en-US", $use_slug = false) {

		$out = array();
		$categories = $this->fetchAll($this->select()->where("locale = ?",$locale));

		if (count($categories) > 0) {
			foreach ($categories as $category) {
				if ($use_slug) {
					$out[$category->slug] = $category->description;
				} else {	
					$out[$category->id] = $category->description;
				}
			}
		}

		return $out;				
	}

	function getCategoryIdBySlug($slug) {
		$category = $this->fetchRow($this->select()->where("slug = ?", $slug));
		if (!is_null($category)) {
			return $category->id;
		} else {
			return null;
		}
	}

	function getCategorySlugById($id) {
		$category = $this->fetchRow($this->select()->where("id = ?", $id));
		if (!is_null($category)) {
			return $category->slug;
		} else {
			return null;
		}
	}

	function clearCategoriesForPost($post_id) {
		$post_categories_table = new BlogPostsCategories();
		$where = $this->getAdapter()->quoteInto("post_id = ?", $post_id);
		$post_categories_table->delete($where);		
	}

	function assignCategoryBySlug($post_id, $category_slug) {
		$category_id = $this->getCategoryIdBySlug($category_slug);
		$post_categories_table = new BlogPostsCategories();
		$where = $this->getAdapter()->quoteInto("post_id = ?", $post_id);
		$where .= $this->getAdapter()->quoteInto(" and category_id = ?", $category_id);

		if (!is_null($category_id) && $post_categories_table->getCountByWhereClause($where) == 0) {
			$post_categories_table->insert(array(
				"category_id" => $category_id,
				"post_id" => $post_id,
			));								
		}
	}

	function getCategorySlugsByPostId($post_id) {
		$slugs = array();
		$post_categories_table = new BlogPostsCategories();
		$post_categories = $post_categories_table->fetchAll($post_categories_table
			->select()->where("post_id = ?", $post_id)
		);
		if (count($post_categories) > 0) {
			foreach ($post_categories as $post_category) {
				$slugs[] = $this->getCategorySlugById($post_category->category_id);
			}
		}
		return $slugs;
	}

	function getCategoryIdsByPostId($post_id) {
		$ids = array();
		$post_categories_table = new BlogPostsCategories();
		$post_categories = $post_categories_table->fetchAll($post_categories_table
			->select()->where("post_id = ?", $post_id)
		);
		if (count($post_categories) > 0) {
			foreach ($post_categories as $post_category) {
				$ids[] = $post_category->category_id;
			}
		}
		return $ids;
	}

	function hasPosts($cat_id) {
		$where = $this->getAdapter()->quoteInto("category_id = ?", $cat_id);
		$join_table = new BlogPostsCategories();
		if ($join_table->getCountByWhereClause($where)) {
			return true;
		} else {
			return false;
		}
	}

	function getParsedSlugsByPostId($post_id, $associative = false) {
		$slugs = $this->getCategorySlugsByPostId($post_id);
		$output = array();
		foreach ($slugs as $slug) {
			$cat_slug_split = explode("-", $slug);
			if ($associative) {
				$output[$cat_slug_split[0]] = $cat_slug_split[1];
			} else {
				$output[] = array('activity' => $cat_slug_split[0], 'subsection' => $cat_slug_split[1]);
			}
		}
		return $output;
	}

}
